<?php
/**
 * 修复用户清单数据
 * @author tangyiwan
 */

ini_set('display_errors', 1);
error_reporting(-1);
ini_set('memory_limit', '512M');
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\ObjRelateWiki as mObjRelateWiki;
use Biz\model\wiki\WikiList as mWikiList;
use Biz\module\Classes\wiki\RelateWiki as clsRelateWiki;
use Doraemon\cache\UserCounter;

//php /data/wwwroot/g_biz/cli/Wiki/repairWikiList.php 0 counter 执行全部计数器
//php /data/wwwroot/g_biz/cli/Wiki/repairWikiList.php 1582647 counter 执行某一个用户的计数器
$uid = 0;
$scene = '';
if (isset($argv[1])) {
    $uid = is_numeric($argv[1]) ? $argv[1] : 0;
    $scene = is_string($argv[2]) ? $argv[2] : 'all';
}
if ($uid) {
    clsRelateWiki::changeUserWikiList($uid);
} else {
    $page = 1;
    $pageSize = 200;
    $uids = [];
    do {
        $limit = [($page - 1) * $pageSize, $pageSize];
        $uids = mObjRelateWiki::select('uid', [
            'AND' => [
                'wiki_id[!]' => 0,
                'uid[!]' => 0,
            ],
            'LIMIT' => $limit,
            'GROUP' => ['uid'],
        ]);
        foreach ($uids as $uid) {
            if ($scene == 'all') {
                var_dump('更新uid：' . $uid);
                clsRelateWiki::changeUserWikiList($uid);
            } else if ($scene == 'counter') {
                $count = mWikiList::getUserWikiCount($uid);
                var_dump('更新uid：' . $uid . '关联的wiki计数器,数量:' . $count);
                UserCounter::setCounter($uid, [UserCounter::TYPE_RELATE_WIKI => $count]);
            }
        }
        $page++;
    } while ($uids);
}

